﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kovan.Model.Entity.UserCenter;

namespace Kovan.DAL.UserCenter
{
    public class DActRoleAuthority : IDAL<act_role_authority>
    {
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public act_role_authority GetModel(long id)
        {
            using (var db = new MysqlContext())
            {
                return db.act_role_authority.FirstOrDefault(c => c.id == id);
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public int Add(act_role_authority model)
        {
            using (var db = new MysqlContext())
            {
                db.act_role_authority.Add(model);
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="roidid">角色ID</param>
        /// <param name="ids">权限ID集合</param>
        /// <returns></returns>
        public int BatchAdd(int roidid, IEnumerable<string> ids)
        {
            using (var db = new MysqlContext())
            {
                foreach (var id in ids)
                {
                    var aid = long.Parse(id);

                    var model = new act_role_authority();
                    model.role_id = roidid;
                    model.authority_id = aid;
                    db.act_role_authority.Add(model);
                }
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public int Delete(long id)
        {
            using (var db = new MysqlContext())
            {
                var model = db.act_role_authority.FirstOrDefault(a => a.id == id);
                db.act_role_authority.Remove(model);
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 删除记录（根据角色ID）
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public int DeleteByRoleID(long roleid)
        {
            using (var db = new MysqlContext())
            {
                var items = db.act_role_authority.Where(a => a.role_id == roleid);
                foreach (var item in items)
                {
                    db.act_role_authority.Remove(item);
                }
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int BatchDelete(IEnumerable<string> ids)
        {
            using (var db = new MysqlContext())
            {
                foreach (var id in ids)
                {
                    var arid = long.Parse(id);
                    var model = db.act_role_authority.FirstOrDefault(a => a.id == arid);
                    if (model != null)
                    {
                        db.act_role_authority.Remove(model);
                    }
                }
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public int Update(act_role_authority model)
        {
            using (var db = new MysqlContext())
            {
                db.Set<act_role_authority>().Attach(model);
                db.Entry(model).State = System.Data.EntityState.Modified;
                return db.SaveChanges();
            }
        }
    }
}
